//
//  DDAccountManagerViewController.m
//  Dingdong
//
//  Created by 程强 on 16/11/11.
//  Copyright © 2016年 程强. All rights reserved.
//

#import "DDAccountManagerViewController.h"
#import "MKUIImagePickerOperation.h"
#import "KSDatePickerView.h"
#import "DDUserManager.h"
#import "DDModifyNicknameViewController.h"

@interface DDAccountManagerViewController ()<UIActionSheetDelegate>
@property (weak, nonatomic) IBOutlet UILabel *userNameLbl;
@property (weak, nonatomic) IBOutlet UILabel *sexLbl;
@property (weak, nonatomic) IBOutlet UILabel *birthLbl;
@property (weak, nonatomic) IBOutlet UIImageView *photoImg;
@property (weak, nonatomic) IBOutlet UILabel *emailLbl;
@property (weak, nonatomic) IBOutlet UITextField *nickNameTxf;
@property (strong, nonatomic) DDUserManager *manager;

@end

@implementation DDAccountManagerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.manager = [DDUserManager new];
    [self layoutAccountView];
}
- (void)layoutAccountView
{
    MyUser;
    self.userNameLbl.text = myUser.uname;
    self.emailLbl.text = myUser.email;
    self.nickNameTxf.text = myUser.nickname;
    _photoImg.layer.masksToBounds = YES;
    _photoImg.layer.cornerRadius =_photoImg.bounds.size.width/2;
    [self.photoImg sd_setImageWithURL:[NSURL URLWithString:myUser.face] placeholderImage:LoadImage(@"photo_select")];
    if (myUser.sex.integerValue == 1) {
        self.sexLbl.text = @"Male";
    }else{
        self.sexLbl.text = @"Female";
    }
    self.birthLbl.text = [ToolKit timeStampTurnDate:FormatString(@"%@", myUser.birthday) withFormat:@"yyyy-MM-dd"];
}
#pragma mark --- IBAction
- (IBAction)setPhotoClick:(id)sender
{
    UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Taking pictures",@"Choose from album", nil];
    sheet.tag = 101;
    [sheet showInView:self.view];
}
- (IBAction)modifyNicknameClick:(id)sender
{
    DDModifyNicknameViewController *vc = StoryBoardViewController(@"User", @"DDModifyNicknameViewController");
    [self.navigationController pushViewController:vc animated:YES];
    [vc modifyNicknameComplete:^(NSString *nickname) {
        self.nickNameTxf.text = nickname;
    }];
}
- (IBAction)setSexClick:(id)sender
{
    UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Male",@"Female", nil];
    sheet.tag = 102;
    [sheet showInView:self.view];
}
- (IBAction)setBirthClick:(id)sender
{
    weak_Self(self);
    KSDatePickerView *datePicker = [[KSDatePickerView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    [datePicker userSelectDateComplete:^(NSDate *date) {
        MyUser;
        NSString *birth = [ToolKit dateToNYRString:date];
        NSString *timeStamp = [ToolKit dateTurnToTimeStamp:date];
        [self updateUserInfo:0 birthday:birth type:3 complete:^{
            weakSelf.birthLbl.text = birth;
            myUser.birthday = @([timeStamp doubleValue]);
        }];
    }];
    [self.view.window addSubview:datePicker];
}
#pragma mark --- Delegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    //头像选择
    if (actionSheet.tag == 101) {
        weak_Self(self);
        MKUIImagePickerOperation *operation = [MKUIImagePickerOperation shareInstance];
        MediaType type = MediaCameraType;
        if (buttonIndex == 0) {

        }else if (buttonIndex ==1){
            type = MediaALAssetType;
        }else{
            return;
        }
        [operation showImagePickerControllerWithType:type edit:YES success:^(UIImagePickerController *imagePickerController) {
            [weakSelf presentViewController:imagePickerController animated:YES completion:^{
                
            }];
        }];
        [operation pickerImageFinished:^(UIImage *originalImage, UIImage *compressionImage) {
            weakSelf.photoImg.image = compressionImage;
            [weakSelf dismissViewControllerAnimated:YES completion:nil];
            [weakSelf uploadUserPhoto:compressionImage];
        }];
        return;
    }
    //性别选择
    NSInteger sex = 0;
    if (actionSheet.tag == 102){
        if (buttonIndex == 0) {
            sex = 1;
        }else if (buttonIndex ==1){
            sex = 0;
        }else{
            return;
        }
        weak_Self(self);
        MyUser;
        [self updateUserInfo:sex birthday:@"" type:1 complete:^{
            if (sex == 0) {
                weakSelf.sexLbl.text =@"Female";
            }else{
                weakSelf.sexLbl.text = @"Male";
            }
            myUser.sex = @(sex);
        }];
        return;
    }

}
- (void)updateUserInfo:(NSInteger)sex birthday:(NSString *)birth type:(NSInteger)type complete:(void(^)())block
{
    [[self.manager updateUserInfoWithSex:sex nickname:@"" birthday:birth type:type]subscribeNext:^(id x) {
        if (block) {
            block();
        }
    } error:^(NSError *error) {
        
    } completed:^{
        
    }];
}
- (void)uploadUserPhoto:(UIImage *)img
{
    MyUser;
    [[self.manager updateUserPhoto:img]subscribeNext:^(id x) {
        NSString *photo = x[@"data"];
        myUser.face = photo;
    } error:^(NSError *error) {
        
    } completed:^{
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
